//
//  FaceAddFriendViewController.m
//  SouthChina
//
//  Created by Mr.Y on 15/1/8.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "FaceAddFriendViewController.h"

@interface FaceAddFriendViewController ()
{
    NSTimer *_countDownTimer;
    int secondsCountDown;

}
@property (retain, nonatomic) UIView * codeImage;
@property (retain, nonatomic) UILabel * codeLabel;
@property (retain, nonatomic) UILabel * timeLabel;
@property (retain, nonatomic) UILabel * promptLabel1;
@property (retain, nonatomic) UILabel * promptLabel2;
@property (retain, nonatomic) UIButton * creatorCodeBtn;
@property (retain, nonatomic) UIButton * writeCodeBtn;
@property (retain, nonatomic) UITextField * writeCodeText;

@end

@implementation FaceAddFriendViewController
@synthesize codeImage,codeLabel,promptLabel1,promptLabel2,creatorCodeBtn,writeCodeBtn,writeCodeText,timeLabel;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addTitleBarWithBackBtn:@"面对面加好友" needReturn:YES];
    UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
    img=[img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    [self.view setBackgroundColor:[UIColor colorWithRed:29/255.0f green:39/255.0f blue:44/255.0f alpha:1]];
    codeImage = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-72.5,90, 145, 30)];
    for (int i = 0; i<6; i++) {
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(25*i,0, 20, 20)];
        image.image = [UIImage imageNamed:@"king_ball"];
        [codeImage addSubview:image];
    }
    [self.view addSubview:codeImage];
    
    codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-72.5,90, 145, 30)];
    codeLabel.text = @"1234567";
    codeLabel.textAlignment = NSTextAlignmentCenter;
    codeLabel.textColor = [UIColor whiteColor];
    codeLabel.backgroundColor = [UIColor clearColor];
    codeLabel.font = [UIFont boldSystemFontOfSize:20.0f];
    codeLabel.hidden = YES;
    [self.view addSubview:codeLabel];
    
    writeCodeText = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-72.5,90, 145, 30)];
    writeCodeText.borderStyle=UITextBorderStyleRoundedRect;
    writeCodeText.placeholder = @"请填写随机验证码";
    writeCodeText.font = [UIFont boldSystemFontOfSize:15.0f];
    writeCodeText.hidden = YES;
    [self.view addSubview:writeCodeText];
    
    promptLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(30,codeImage.frame.size.height+codeImage.frame.origin.y+10, SCREEN_WIDTH-60, 21)];
    promptLabel1.text = @"您的随机验证码";
    promptLabel1.textAlignment = NSTextAlignmentCenter;
    promptLabel1.textColor = [UIColor whiteColor];
    promptLabel1.backgroundColor = [UIColor clearColor];
    promptLabel1.font = [UIFont boldSystemFontOfSize:14.0f];
    [self.view addSubview:promptLabel1];

    promptLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10,promptLabel1.frame.size.height+promptLabel1.frame.origin.y+10, SCREEN_WIDTH-20, 60)];
    promptLabel2.text = @"       这是一个神奇的功能，通过这个功能你能找到身边的好朋友，只要对方输入你的随机验证码，就可以加为好友尽情畅聊。";
    promptLabel2.numberOfLines = 3;
    promptLabel2.textColor = [UIColor whiteColor];
    promptLabel2.backgroundColor = [UIColor clearColor];
    promptLabel2.font = [UIFont boldSystemFontOfSize:12.0f];
    [self.view addSubview:promptLabel2];

    //生成验证码
    creatorCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,self.view.frame.size.height-95, (SCREEN_WIDTH)-20, 35)];
    [creatorCodeBtn setBackgroundImage:img forState:UIControlStateNormal];
    [creatorCodeBtn addTarget:self action:@selector(creatorCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [creatorCodeBtn setTitle:@"生成验证码" forState:UIControlStateNormal];
    [self.view addSubview:creatorCodeBtn];
    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,7, (creatorCodeBtn.frame.size.width)-20, 20)];
    timeLabel.text = @"300秒后重新生成";
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.hidden = YES;
    timeLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [creatorCodeBtn addSubview:timeLabel];
    //填写验证码
    writeCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,self.view.frame.size.height-50, (SCREEN_WIDTH)-20, 35)];
    [writeCodeBtn setBackgroundImage:img forState:UIControlStateNormal];
    [writeCodeBtn addTarget:self action:@selector(writeCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [writeCodeBtn setTitle:@"填写验证码" forState:UIControlStateNormal];
    [self.view addSubview:writeCodeBtn];

    // Do any additional setup after loading the view.
}
-(void)timeFireMethod{
    secondsCountDown--;
    timeLabel.text = [NSString stringWithFormat:@"%d秒后重新生成",secondsCountDown];
    [creatorCodeBtn setTitle:@"" forState:UIControlStateNormal];
    timeLabel.hidden = NO;
    if(secondsCountDown==0){
        creatorCodeBtn.enabled = YES;
        [_countDownTimer invalidate];
        timeLabel.text = @"生成验证码";
    }
}
-(void)creatorCodeBtn:(id)sender
{
    creatorCodeBtn.frame = CGRectMake(10,self.view.frame.size.height-50, (SCREEN_WIDTH)-20, 35);
    [creatorCodeBtn setTitle:@"" forState:UIControlStateNormal];
    NSDictionary *map = @{};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"createFriendNumber" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            timeLabel.hidden = NO;
            creatorCodeBtn.enabled = NO;
            codeLabel.hidden = NO;
            codeImage.hidden = YES;
            writeCodeBtn.hidden = YES;
            secondsCountDown = 300;
            _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            codeLabel.text= [NSString stringWithFormat:@"%@",[data objectForKey:@"number"]];

        }
        else{
            [self showAlerHUD:@"获取验证码失败！"];
        }
    }];

}

-(void)writeCodeBtn:(id)sender
{
    writeCodeText.hidden = NO;
    codeImage.hidden = YES;
    codeLabel.hidden = YES;
    promptLabel1.hidden = YES;
    promptLabel2.hidden = YES;
    creatorCodeBtn.hidden = YES;
    [writeCodeBtn setTitle:@"添加好友" forState:UIControlStateNormal];
    [writeCodeBtn addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];

}
-(void)addFriend:(id)sender
{
    NSDictionary *map = @{@"number":writeCodeText.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"validateFriendNumber" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        NSLog(@"面对面添加好友结果：%@",[data objectForKey:@"resultInfo"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success && data && [data objectForKey:@"result"])
        {
            [self showAlerHUD:[data objectForKey:@"resultInfo"]];
        }
        else{
            [self showAlerHUD:[data objectForKey:@"resultInfo"]];
        }
    }];

}

@end
